﻿namespace SmartSoft.Controls.ChildGridControls
{
    using SmartSoft;
    using SmartSoft.Controls;
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class OptionCell : DataGridViewTextBoxCell
    {
        private AppInstance.TextControlLength ie_UserTextLength;
        private AppInstance.TextControlTrimMode ie_UserTrimMode = AppInstance.TextControlTrimMode.Right;
        private string is_InputMask = "";
        private Color oDisabledForeColor = Color.Blue;

        public override object Clone()
        {
            OptionCell cell = base.Clone() as OptionCell;
            if (cell != null)
            {
                cell.DisabledForeColor = this.DisabledForeColor;
                cell.UserTrimMode = this.UserTrimMode;
                cell.UserTextLength = this.UserTextLength;
                cell.InputMask = this.InputMask;
            }
            return cell;
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            TextOption editingControl = base.DataGridView.EditingControl as TextOption;
            if (editingControl != null)
            {
                editingControl.BorderStyle = BorderStyle.None;
                editingControl.DisabledForeColor = this.DisabledForeColor;
                editingControl.UserTrimMode = this.UserTrimMode;
                editingControl.UserTextLength = this.UserTextLength;
                editingControl.InputMask = this.InputMask;
                string str = initialFormattedValue as string;
                if (str == null)
                {
                    editingControl.Text = string.Empty;
                }
                else
                {
                    editingControl.Text = str;
                }
            }
        }

        private void OnCommonChange()
        {
            if (((base.DataGridView != null) && !base.DataGridView.IsDisposed) && !base.DataGridView.Disposing)
            {
                if (base.RowIndex == -1)
                {
                    base.DataGridView.InvalidateColumn(base.ColumnIndex);
                }
                else
                {
                    base.DataGridView.UpdateCellValue(base.ColumnIndex, base.RowIndex);
                }
            }
        }

        private bool OwnsEditingOptionControl(int rowIndex)
        {
            if ((rowIndex == -1) || (base.DataGridView == null))
            {
                return false;
            }
            OptionEditing editingControl = base.DataGridView.EditingControl as OptionEditing;
            return ((editingControl != null) && (rowIndex == editingControl.EditingControlRowIndex));
        }

        internal void SetInputMask(int rowIndex, string value)
        {
            this.is_InputMask = value;
            if (this.OwnsEditingOptionControl(rowIndex))
            {
                this.EditingOptionControl.InputMask = value;
            }
        }

        public Color DisabledForeColor
        {
            get
            {
                return this.oDisabledForeColor;
            }
            set
            {
                if (this.oDisabledForeColor != value)
                {
                    this.oDisabledForeColor = value;
                    if (this.OwnsEditingOptionControl(base.RowIndex))
                    {
                        this.EditingOptionControl.DisabledForeColor = value;
                    }
                }
            }
        }

        private OptionEditing EditingOptionControl
        {
            get
            {
                return (base.DataGridView.EditingControl as OptionEditing);
            }
        }

        public override System.Type EditType
        {
            get
            {
                return typeof(OptionEditing);
            }
        }

        public string InputMask
        {
            get
            {
                return this.is_InputMask;
            }
            set
            {
                if (this.is_InputMask != value)
                {
                    this.SetInputMask(base.RowIndex, value);
                    this.OnCommonChange();
                }
            }
        }

        public AppInstance.TextControlLength UserTextLength
        {
            get
            {
                return this.ie_UserTextLength;
            }
            set
            {
                if (this.ie_UserTextLength != value)
                {
                    this.ie_UserTextLength = value;
                    if (this.OwnsEditingOptionControl(base.RowIndex))
                    {
                        this.EditingOptionControl.UserTextLength = value;
                    }
                }
            }
        }

        public AppInstance.TextControlTrimMode UserTrimMode
        {
            get
            {
                return this.ie_UserTrimMode;
            }
            set
            {
                if (this.ie_UserTrimMode != value)
                {
                    this.ie_UserTrimMode = value;
                    if (this.OwnsEditingOptionControl(base.RowIndex))
                    {
                        this.EditingOptionControl.UserTrimMode = value;
                    }
                }
            }
        }

        public override System.Type ValueType
        {
            get
            {
                return typeof(string);
            }
        }
    }
}

